Skip to content

Proposed: _.call2, _.mapArgs, and _.mapArgsWith#32

Merged
fogus merged 5 commits intodocumentcloud:masterfrom
raganwald:master
May 22, 2013
Merged

Proposed: _.call2, _.mapArgs, and _.mapArgsWith#32
fogus merged 5 commits intodocumentcloud:masterfrom
raganwald:master

Conversation

@raganwald
Copy link
Copy Markdown
Contributor

_.call2 is a utility-function that subsumes calling, partial application, and currying. You can write:

_.call2(fun, 1, 2)
_.call2(fun, 1)(2)
_.call2(fun)(1, 2)
_.call2(fun)(1)(2)

This facilitates a lot of other combinator writing.

_.mapArgs takes a function and returns a version that performs a map on its arguments. Example:

function echo () {
  return [].slice.call(arguments, 0);
}
function square (n) { return n * n; }
_mapArgs(echo, square)(1, 2, 3) //=> [1, 4, 9]

_.mapArgsWith is the same thing, with its arguments flipped to facilitate partial application of the mapper as a decorator.

These functions are integral to a post I'm writing about underscore-contrib :-)

@fogus
Copy link
Copy Markdown
Member

fogus commented May 14, 2013

One (strong) way that I diverge philosophically from _ is in my view on "magic" arguments. With that said, call2 makes me feel a little queasy. I'd prefer different names for different tasks. Explicit is better than implicit?

Looking over your mappy function one thing that jumped out at me was that baseMapArgs is a variant of _.walk.map, now available in _.contrib. :-)

@raganwald
Copy link
Copy Markdown
Contributor Author

With call2, I'm trying to emulate the semantics of languages where everything is curried by default, and writing foo(bar, blitz) is syntactic sugar for writing foo(bar)(blitz). When you then consider call2(foo, bar, blitz), it follows that since call2 itself is curried by default, you get the behaviour written above.

However, I can see the argument that _.contrib is a different world, so I will make it rain accordingly.

@raganwald
Copy link
Copy Markdown
Contributor Author

I've reviewed _.walk.map, and I'm not sure how to use _walk.map on it, except possibly to replace __map.call on arguments. Could you elaborate?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants